﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Web.Routing;
using System.Globalization;
using System.Web;
using System.Security.Cryptography;

namespace MVCCentral.Framework
{
    public static class StringExtensions
    {
        #region SEO Extensions
        public static string ToSEOString(this string source)
        {
            string result = string.Empty;
            if (!string.IsNullOrEmpty(source))
            {
                result = source.Replace(" ", "_");
            }
            return result;
        }

        public static string FromSEOString(this string source)
        {
            string result = string.Empty;
            if (!string.IsNullOrEmpty(source))
            {
                result = source.Replace("_", " ");
            }
            return result;
        }
        #endregion


        /// <summary>
        /// This will chop the string at a specified character length.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string StopAtChars(this string source, int charLength)
        {
            string result = source.Substring(0, charLength);
            return result;
        }

        /// <summary>
        /// Chop the current string at the specified word count.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="wordLength"></param>
        /// <returns></returns>
        public static string StopAtWords(this string source, int wordCount)
        {
            StringBuilder sb = new StringBuilder();

            string[] words = source.Split(' ');
            int len = (wordCount > words.Length) ? words.Length : wordCount;
            for (int i = 0; i < len; i++)
            {
                sb.Append(words[i] + " ");
            }
            return sb.ToString();
        }


        public static string FormatFilename(this string source)
        {
            string result = string.Empty;
            if (!string.IsNullOrEmpty(source))
            {
                result = source.Replace(" ", "_");
            }
            return result;
        }

        public static string TrimTrailingComma(this string source)
        {
            int commaPos = source.Trim().LastIndexOf(",");
            string result = source.Trim();
            if (!string.IsNullOrEmpty(result))
            {
                if (commaPos == result.Length - 1)
                {
                    result = result.Substring(0, commaPos);
                }
            }
            return result;
        }

        public static string FormatFileSize(this long source)
        {
            return FileSizer.FileSize(source);
        }

        public static string FormatFileSize(this int? source)
        {
            long size = (long)source;
            return FileSizer.FileSize(size);
        }

        public static string FormatFileSize(this int source)
        {
            long size = (long)source;
            return FileSizer.FileSize(size);
        }

        public static StringBuilder AppendRandomString(this StringBuilder builder, int len)
        {
            var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
            var data = new byte[len];

            RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider();
            crypto.GetNonZeroBytes(data);

            foreach (byte b in data)
            {
                builder.Append(chars[b % (chars.Length - 1)]);
            }

            return builder;
        }

        public static string ConvertMonthNumberToName(this int source)
        {
            string result = string.Empty;
            string[] months = new string[] { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

            result = months[source-1];

            return result;
        }

    }
}
